<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://isofttime.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\rob\model;

use think\Model;



class RobEventFans extends Model
{

  
  protected $insert = ['status'=>1];  
  protected $autoWriteTimestamp = true;

  public function info($openid,$field=true){
      
      if (is_numeric($openid)){
        $map['id']=$openid;
      }else{
        $map['openid']=$openid;
      }
      $fans = $this->field($field)->where($map)->find();
     
      return  $fans;
  }

  public function add($data)
  {
    
     //活动有效性
     $event=model('rob/RobEvent')->check($data['back']);
 
     if (!isset($event['title'])) return  ['message'=>$event];

     $fans=$this->info($data['openid']);
   
     if ($fans) { 
      $message='对不起，'.$fans['nickname'].',你已于'.date("Y-h-d H:i",$fans['create_time']).'参加'.$event['title'].'活动！';
     }else{
        $add=[];
        $add['aid']=$data['aid'];
        $add['appid']=$data['appid'];
        $add['event']=$event['id'];
        $add['openid']=$data['openid'];
        $add['nickname']=$data['nickname'];
        $res=$this->save($add);
        if ($res)$message=$data['nickname'].'成功参加'.$event['title'].'活动';
     }

      return  ['message'=>$message];
  }

 



 
    
   
}